**
**  This file corresponds to the paper:
**
**  Aidt, Toke, Zareh Asatryan, Lusine Badalyan and Friedrich Heinemann. 2018.
**        Vote Buying or (Political) Business (Cycles) as Usual?
**        The Review of Economics and Statistics, forthcoming.
**
**  This DO file replicates the first (macro) part of the analysis
**  For the DO files replicating the second(micro) and third(micro-macro) parts of the analysis, see do_votebuying_micro.do and do_votebuying_micro-macro.do.
**
**  Date: 07 Dec 2018
**       

clear all
set more off, perm
set maxvar  32767
set matsize 11000

** Change directory to local folder
cd "C:\Users\user\Desktop\Vote Buying\0_RESTAT"
* if not already ssc install the following programs: xtivreg2 outreg2 tabout xtabond2 egenmore coefplot

** Load data (country-month level data on M1, 1975-2015)
use m1_monthly_world.dta, clear 
xtset country_id yearmonth


** Define locals

tab month, gen(month)
tab year, gen(year)
local time month1-month12 year1-year44

gen country_month = country_id*100+month
tab country_month, gen(cm_dummy)
gen country_year = country_id*10000+year
tab country_year, gen(cy_dummy)
gen year_month = year*100+month
tab year_month, gen(ym_dummy)
local dummies0 
local dummies  cm_dummy*  cy_dummy*  ym_dummy*

local el0 election
local el1 election l1_election f1_election 
local el2 election l2_election l1_election f1_election f2_election 

local lag_0
local lag_1 l1_money_gr 
local lag_2 l1_money_gr l2_money_gr 
local lag_3 l1_money_gr l2_money_gr l3_money_gr 
local lag_4 l1_money_gr l2_money_gr l3_money_gr l4_money_gr 
local lag_5 l1_money_gr l2_money_gr l3_money_gr l4_money_gr l5_money_gr 
local lag_6 l1_money_gr l2_money_gr l3_money_gr l4_money_gr l5_money_gr l6_money_gr

local lag_sad_0
local lag_sad_1 l1_money_sad_gr 
local lag_sad_2 l1_money_sad_gr l2_money_sad_gr 
local lag_sad_3 l1_money_sad_gr l2_money_sad_gr l3_money_sad_gr 
local lag_sad_4 l1_money_sad_gr l2_money_sad_gr l3_money_sad_gr l4_money_sad_gr 
local lag_sad_5 l1_money_sad_gr l2_money_sad_gr l3_money_sad_gr l4_money_sad_gr l5_money_sad_gr 
local lag_sad_6 l1_money_sad_gr l2_money_sad_gr l3_money_sad_gr l4_money_sad_gr l5_money_sad_gr l6_money_sad_gr

local lag_int_0 
local lag_int_3 l1_int l2_int l3_int

local controls0
local controls  gdp_pc_gr ln_gdp_pc inflation ln_exchange_rate resource_gdp 
local controls1 gdp_pc_gr ln_gdp_pc inflation ln_exchange_rate resource_gdp 
local controls2 gdp_pc_gr ln_gdp_pc inflation ln_exchange_rate resource_gdp polity2 newdem
local controls3 gdp_pc_gr ln_gdp_pc inflation ln_exchange_rate resource_gdp polity2 newdem tbill l1_tbill tbill_gr


** Define samples
foreach c in 1 2 3{
xtreg money_gr election money_sad_gr electionday `lag_3' `lag_sad_3' `controls`c'' `dummies0' `time' if oecd2009==0, fe cluster(country_id) robust
gen fix_sample`c'=1 if e(sample)==1
xtreg money_gr election money_sad_gr electionday `lag_3' `lag_sad_3' `controls`c'' `dummies0' `time', fe cluster(country_id) robust
gen fix_sample`c'_all=1 if e(sample)==1
}

** Table 1 Basline: columns 1-4
foreach m in _ {
foreach o in 2009{
foreach c in 2 3{
foreach e in election electionday {
foreach i in 3{
xtivreg2 money`m'gr `e' `lag`m'`i'' `controls`c'' `time' if oecd`o'==0, fe cluster(country_id) robust
outreg2 using base, label e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
}
}
}
}
}

** Table 1: columns 5 & 10
foreach m in _ {
foreach c in 2 3{
foreach e in election{
foreach i in 3{ 
xtreg money`m'gr `e' `lag`m'`i'' `controls`c'' `dummies0' `time' if fix_sample`c'==1, fe cluster(country_id) robust
outreg2 using base, label e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
}
}
}
}

** Table 1: columns 6-9 & 11-14
foreach m in _  _sad_{
foreach c in 2 3{
foreach e in election electionday{
foreach i in 3{ 
xtreg money`m'gr `e' `lag`m'`i'' `controls0' `dummies'  `time' if fix_sample`c'==1, fe cluster(country_id) robust
outreg2 using base, label e(F) ctitle(`m') tex(frag) dec(4) excel drop(month1 month2 month3 month4 month5 month6 month7 month8 month9 month10 month11 month12  year1 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 year12 year13 year14 year15 year16 year17 year18 year19 year20 year21 year22 year23 year24 year25 year26 year27 year28 year29 year30 year31 year32 year33 year34 year35 year36 year37 year38 year39 year40 year41 year42 year43 year44) sortvar(election electionday electiondays l3_election l2_election l1_election f1_election f2_election f3_election  electiondays l3_electiondays l2_electiondays l1_electiondays f1_electiondays f2_electiondays f3_electiondays  electionday l3_electionday l2_electionday l1_electionday f1_electionday f2_electionday f3_electionday  l1_money_gr l2_money_gr l3_money_gr l4_money_gr l5_money_gr l6_money_gr ln_gdp_pc gdp_pc_gr inflation ln_exchange_rate resource_gdp) append
}
}
}
}


** Table 2 Refinancing Rate: columns 1-6
foreach c in 2{
foreach i in 3{
foreach e in election l2_election l1_election f1_election f2_election{
xtivreg2 interest_rate `el2' `lag_int_`i'' `controls`c'' `time', fe cluster(country_id) robust
outreg2 using base_intrate, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth `controls3' `lag_6' `lag_sad_6') append
xtivreg2 interest_rate `e' `lag_int_`i'' `controls`c'' `time' if e(sample)==1, fe cluster(country_id) robust
outreg2 using base_intrate, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth `controls3' `lag_6' `lag_sad_6') append
}
}
}

** Table 2 Higher Money: columns 7-9
foreach v in m1_m2 M2growth M3growth{
foreach o in 2009{
foreach c in 2{
foreach i in 0{
xtivreg2 `v' election `lag`i'' `controls`c'' `time' if oecd`o'==0, fe cluster(country_id)
outreg2 using base_highermoney, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth `controls3' `lag_6' `lag_sad_6') append
}
}
}
}


** Figure 1.A CB Independence (and Table A.5, column 1)
foreach m in _{
foreach c in 2{
foreach v in lvau{
xtreg money`m'gr  i.election##c.`v' `lag`m'3' `controls`c'' `time', fe cluster(country_id) robust
gen `v'_sample=`v' if e(sample)==1
outreg2 using base_CBIndep, e(F) tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth `controls3' `lag_6' `lag_sad_6') append
margins, dydx(election) at(`v'=(0(0.05)1))
marginsplot, yline(0) legend() addplot(hist `v'_sample, mcolor(gs12) below yaxis(2) yscale(range(1(4)20) alt axis(2)))
graph export cb_indep_`o'c`c'_m`m'.pdf, as(pdf) replace
drop `v'_sample
}
}
}

** Figure 1.B Democracy (and Table A.7, column 2)
foreach m in _ {
foreach c in 2 {
foreach e in election {
foreach i in 3{ 
foreach v in polity2{
xtreg money`m'gr  i.`e'##c.`v'##c.`v' `lag`m'`i'' `controls`c'' `dummies0' `time' if fix_sample`c'_all==1, fe cluster(country_id) robust
gen `v'_sample=`v' if e(sample)==1
outreg2 using base_polity, e(F) tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth dateleg dateexec president_vote party_vote `controls3' `lag_6' `lag_sad_6') append
margins, dydx(`e') at(`v'=(0(0.05)1))
marginsplot, yline(0) legend() addplot(hist `v'_sample, mcolor(gs12) below yaxis(2) yscale(range(1(4)20) alt axis(2)))
graph export P`v'_`m'_`o'_`i'_`c'_fix.pdf, as(pdf) replace
drop `v'_sample
}
}
}
}
}

** Figure 2 Election Monitoring (and Table A.6)
foreach m in _ {
foreach c in 2 {
foreach e in election {
foreach i in 3{ 
foreach v in  diem_a1_osce diem_mean_r21cheat diem_max_r21cheat qed_sr21cheat{
xtreg  money`m'gr i.`e'##c.`v' `lag`m'`i'' `controls`c'' `time' if oecd2009==0, fe cluster(country_id) robust
gen `v'_sample=1 if e(sample)==1
outreg2 using election_monitoring,  e(F) tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' diem_a1_osce diem_mean_r21cheat diem_max_r21cheat qed_sr21cheat `controls3' `lag_6' `lag_sad_6') append
margins, dydx(`e') at(`v'=(0(0.1)1))
marginsplot, yline(0)
graph export Monitor`v'.pdf, as(pdf) replace
xtreg  money`m'gr `e' `lag`m'`i'' `controls`c'' `time' if oecd2009==0 & `v'_sample==1, fe cluster(country_id) robust
outreg2 using election_monitoring,  e(F) tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' diem_a1_osce diem_mean_r21cheat diem_max_r21cheat qed_sr21cheat `controls3' `lag_6' `lag_sad_6') append
drop `v'_sample
}
}
}
}
}                            

** Figure 3.A and 3.C Vote Margins (and Table A.7, columns 3-4)
foreach o in 0 1{
foreach m in _ {
foreach c in 2{
foreach e in dateleg{
foreach i in 3{ 
foreach v in party_vote{
xtreg money`m'gr  i.`e'##c.`v'##c.`v' `lag`m'`i'' `controls`c'' `dummies0' `time' if oecd2009==`o', fe cluster(country_id) robust
gen `v'_sample=`v' if e(sample)==1
outreg2 using base_voteshare, e(F) tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth dateleg dateexec president_vote party_vote `controls3' `lag_6' `lag_sad_6') append
margins, dydx(`e') at(`v'=(0(2.5)100))
marginsplot, yline(0) legend() addplot(hist `v'_sample, mcolor(gs12) below yaxis(2) yscale(range(0(.01).15) alt axis(2)))
graph export Vote`v'_`e'_`o'.pdf, as(pdf) replace
drop `v'_sample
}
}
}
}
}
}

** Figure 3.B and 3.D Vote Margins (and Table A.7, columns 5-6)
foreach o in 0 1{
foreach m in _ {
foreach c in 2{
foreach e in dateexec{
foreach i in 3{ 
foreach v in president_vote{
xtreg money`m'gr  i.`e'##c.`v'##c.`v' `lag`m'`i'' `controls`c'' `dummies0' `time' if oecd2009==`o', fe cluster(country_id) robust
gen `v'_sample=`v' if e(sample)==1
outreg2 using base_voteshare, e(F) tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth dateleg dateexec president_vote party_vote `controls3' `lag_6' `lag_sad_6') append
margins, dydx(`e') at(`v'=(0(2.5)100))
marginsplot, yline(0) legend() addplot(hist `v'_sample, mcolor(gs12) below yaxis(2) yscale(range(0(.01).15) alt axis(2)))
graph export Vote`v'_`e'_`o'.pdf, as(pdf) replace
drop `v'_sample
}
}
}
}
}
}

** Table A.1 Sample stats
xtivreg2 money_gr election `lag_3' `controls2' `time' if oecd2009==1, fe cluster(country_id) robust
tabout country if e(sample)==1  using sample_oecd.txt, replace
xtivreg2 money_gr election `lag_3' `controls2' `time' if oecd2009==0, fe cluster(country_id) robust
tabout country if e(sample)==1  using sample_nonoecd.txt, replace

** Table A.2 Summary stats
estpost sum  money_gr money_sad_gr M2growth M3growth m1_m2 election dateleg dateexec electionday gdp_pc_gr ln_gdp_pc inflation ln_exchange_rate resource_gdp polity2 newdem tbill tbill_gr interest_rate lvau president_vote party_vote diem_a1_osce diem_mean_r21cheat diem_max_r21cheat qed_sr21cheat
esttab using summary_stats_macro.txt, label cells("count mean sd min max") replace

** Table A.3 Cycles
foreach m in _{
foreach c in 2{
foreach i in 3{
foreach e in election l2_election l1_election f1_election f2_election{
xtivreg2 money`m'gr `el2' `lag`m'`i'' `controls`c'' `time' if oecd2009==0 & fix_sample2==1, fe cluster(country_id) robust
outreg2 using base_cycle, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
xtivreg2 money`m'gr `e' `lag`m'`i'' `controls`c'' `time' if e(sample)==1, fe cluster(country_id) robust
outreg2 using base_cycle, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
xtivreg2 money`m'gr `el2' `lag`m'`i'' `controls`c'' `time' if oecd2009==1, fe cluster(country_id) robust
outreg2 using base_cycle, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
xtivreg2 money`m'gr `e' `lag`m'`i'' `controls`c'' `time' if e(sample)==1, fe cluster(country_id) robust
outreg2 using base_cycle, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
}
}
}
}

** Table A.4 Legislative and Executive
foreach m in _{
foreach c in 2{
foreach i in 3{
foreach e in election dateleg_only dateexec_only elecboth {
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==0 & fix_sample2==1, fe cluster(country_id) robust
outreg2 using base_legelec, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth `controls3' `lag_6' `lag_sad_6') append
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==1, fe cluster(country_id) robust
outreg2 using base_legelec, e(F) ctitle(`o'; `m') tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday dateleg_only dateexec_only elecboth `controls3' `lag_6' `lag_sad_6') append
}
}
}
}

** Table A.5 Robustness
foreach v in money_gr{
_pctile money_gr, p(1  5  95 99)
return list
gen `v'_outlier1=1  if `v'>=`r(r1)'  & `v'<=`r(r4)'
gen `v'_outlier5=1  if `v'>=`r(r2)'  & `v'<=`r(r3)'
}
foreach m in _ {
foreach c in 2{
foreach e in election{
xtivreg2 money`m'gr `e' `lag`m'0' `controls`c'' `time' if oecd2009==0, fe cluster(country_id) robust
est store a1
xtivreg2 money`m'gr `e' `lag`m'1' `controls`c'' `time' if oecd2009==0, fe cluster(country_id) robust
est store a2
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==0, fe cluster(country_id) robust
est store a3
xtreg    money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==0, fe vce(bootstrap, reps(1000))
est store a4
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==0 & money`m'gr_outlier1==1 , fe cluster(country_id) robust
est store a5
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==0 & money`m'gr_outlier5==1 , fe cluster(country_id) robust
est store a6
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd1975==0, fe cluster(country_id) robust
est store a7
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2017==0, fe cluster(country_id) robust
est store a8
xtivreg2 money`m'gr `e' `lag`m'6' `controls`c'' `time' if oecd2009==0, fe cluster(country_id) robust
est store a9
xtivreg2 money_gr l1_money_gr  (l1_money_gr = l2_money_gr l3_money_gr l4_money_gr l5_money_gr l6_money_gr) election `controls`c'' `time' if oecd2009==0, fe first gmm
est store a10
xtivreg2 money_gr l1_money_gr l2_money_gr l3_money_gr (l1_money_gr l2_money_gr l3_money_gr = l4_money_gr l5_money_gr l6_money_gr) election `controls`c'' `time' if oecd2009==0, fe first gmm
est store a11
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd1975==1, fe cluster(country_id) robust
est store a12
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2009==1, fe cluster(country_id) robust
est store a13
xtivreg2 money`m'gr `e' `lag`m'3' `controls`c'' `time' if oecd2017==1, fe cluster(country_id) robust
est store a14
outreg2 [a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14] using base_robust, e(F) ctitle() tex(frag) dec(4) excel drop(`time' `dummies') sortvar(`el2' electionday `controls3' `lag_6' `lag_sad_6') append
}
}
}

clear
exit
